<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="字典名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.type" placeholder="字典类型" clearable />
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.value" placeholder="字典值" clearable />
      </el-form-item>
      <el-form-item>
        <el-button size="medium" type="primary" @click="getDataList()"
          >查询</el-button
        >
        <el-button size="medium" type="primary" @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button
          type="danger"
          size="medium"
          :disabled="dataListSelections.length <= 0"
          @click="deleteHandle()"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      style="width: 100%"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      />
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="字典名称"
      />
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="字典类型"
      />
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
        label="字典码"
      />
      <el-table-column
        prop="value"
        header-align="center"
        align="center"
        label="字典值"
      />
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序"
      />
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注"
      />
      <el-table-column
        prop="delFlag"
        header-align="center"
        align="center"
        label="删除标记"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.delFlag === -1" size="small">已删除</el-tag>
          <el-tag
            v-else-if="scope.row.delFlag === 0"
            size="small"
            type="success"
            >正常</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import AddOrUpdate from "./sysdict-add-or-update"
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        key: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  activated() {
    this.getDataList()
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      const data = {
        msg: "success",
        code: 0,
        page: {
          totalCount: 976,
          pageSize: 10,
          totalPage: 98,
          currPage: 1,
          list: [
            {
              id: 1,
              name: "民族",
              type: "NATION_TYPE",
              code: "01",
              value: "汉",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 2,
              name: "民族",
              type: "NATION_TYPE",
              code: "02",
              value: "蒙古",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 3,
              name: "民族",
              type: "NATION_TYPE",
              code: "03",
              value: "回",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 4,
              name: "民族",
              type: "NATION_TYPE",
              code: "04",
              value: "藏",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 5,
              name: "民族",
              type: "NATION_TYPE",
              code: "05",
              value: "维吾",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 6,
              name: "民族",
              type: "NATION_TYPE",
              code: "06",
              value: "苗",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 7,
              name: "民族",
              type: "NATION_TYPE",
              code: "07",
              value: "彝",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 8,
              name: "民族",
              type: "NATION_TYPE",
              code: "08",
              value: "壮",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 9,
              name: "民族",
              type: "NATION_TYPE",
              code: "09",
              value: "布依",
              orderNum: 0,
              remark: null,
              delFlag: 0
            },
            {
              id: 10,
              name: "民族",
              type: "NATION_TYPE",
              code: "10",
              value: "朝鲜",
              orderNum: 0,
              remark: null,
              delFlag: 0
            }
          ]
        }
      }
      this.dataListLoading = true
      this.dataList = data.page.list
      this.totalPage = data.page.totalCount
      this.dataListLoading = false
      // this.$http({
      //   url: this.$http.adornUrl("/sys/sysdict/list"),
      //   method: "get",
      //   params: this.$http.adornParams({
      //     page: this.pageIndex,
      //     limit: this.pageSize,
      //     value: this.dataForm.value,
      //     type: this.dataForm.type,
      //     name: this.dataForm.name,
      //   }),
      // }).then(({ data }) => {
      //   if (data && data.code === 0) {
      //     this.dataList = data.page.list;
      //     this.totalPage = data.page.totalCount;
      //   } else {
      //     this.dataList = [];
      //     this.totalPage = 0;
      //   }
      //   this.dataListLoading = false;
      // });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
          return item.id
        })
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/sys/sysdict/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scope>
</style>
